#ifndef DATACENTERPRIVATE_H
#define DATACENTERPRIVATE_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief 数据中心模块解析
*/
#include "sys_inc.h"
#include "tools/m_mutex.h"
#include "rapidjson/document.h"
#include "paho.mqtt/MQTTAsync.h"
#include "def.h"

typedef std::map<std::string, DC::ModelItems> Models;
typedef std::map<std::string, DC::DevInfos> PortDevs;
typedef std::map<std::string, PortDevs> ModelDevs;

struct Topics {
    std::string get_modelschema;
    std::string delete_model;
    std::string get_model;
    std::string set_model;
    std::string get_dev_guid;
    std::string register_dev;
    std::string get_register;
    std::string unregister_dev;
    std::string get_realtime_data;
    std::string get_uartinfo;
    std::string set_uart;
};

typedef struct TagUartInfos {
    std::string uart_name;
    std::string mode;
    std::string appname;
} UartDevInfos;

struct strLess {
    bool operator()( const char * s1, const char * s2 ) const {
        return strcmp( s1, s2 ) < 0;
    }
};

class DataCenter;
class DataCenterPrivate {
public:
    typedef bool (DataCenterPrivate::*PFN_MsgHandler) (const rapidjson::Document &msg);
    typedef std::map<const char *, PFN_MsgHandler, strLess> mapMsghandler;

    DataCenterPrivate(DataCenter *p);
    ~DataCenterPrivate();
    DataCenter *p_;
    uint32_t token_;
    Topics *topics_;
    std::string code_;
    std::list<std::string> model_names_;
    Models models_;
    ModelDevs model_devs_;
    UartDevInfos uart_infos_;
    MSem query_model_schema_sem_;
    MSem query_register_sem_;
    MSem query_uartinfo_sem_;
    bool exited_{false};
    std::string user_;
    std::string passwd_;
    uint16_t keep_alive_;
    mapMsghandler handlers_;
    MQTTAsync client_;
    std::string client_id_;
    MSem sem_login_;
    std::string returntoken_;
    std::string returnstatus_;

    int init(const char *ip, uint16_t port, const char *user, const char *passwd, const char *clientId);
    void deinit(void);
    const std::string &clientId(void) const { return client_id_; }
    int login(const char *user_name, const char *passwd, uint16_t keep_alive);
    int login(void);
    int logout(void);
    bool logined(void) const;
    int subTopic(const char *topic, int qos = 0);
    int unsubTopic(const char *topic);
    int pubTopic(const char *topic, const char *msg=nullptr, int qos=0);
    bool registerHandler(const char *topic, PFN_MsgHandler handler);
    void unregisterHandler(const char *topic);
    bool registered(const char *topic) const;
    void registerGetModelSchema(void);
    void registerGetRegister(void);
    void registerDeleteModel(void);
    void registerGetModel(void);
    void registerSetModel(void);
    void registerGetDevGUID(void);
    void registerRegisterDev(void);
    void registerListenDevRegisterNotify(void);
    void registerUnregisterDev(void);
    void registerGetUartInfo(void);
    void registerSetUart(void);
    static int  msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message);
    static void onConnect(void* context, MQTTAsync_successData* response);
    static void onConnectFailure(void* context, MQTTAsync_failureData* response);
    bool _onGetModelSchema(const rapidjson::Document &msg);
    bool _onGetModel(const rapidjson::Document &msg);
    bool _onSetModel(const rapidjson::Document &msg);
    bool _onDelModel(const rapidjson::Document &msg);
    bool _onGetDevGUID(const rapidjson::Document &msg);
    bool _onRegisterDev(const rapidjson::Document &msg);
    bool _onGetRegister(const rapidjson::Document &msg);
    bool _onUnregisterDev(const rapidjson::Document &msg);
    bool _onRegisterDevNotify(const rapidjson::Document &msg);
    bool _updateDevGUID(const rapidjson::Value &body);
    bool _onGetUartInfo(const rapidjson::Document &msg);
    bool _onSetUart(const rapidjson::Document &msg);
    uint32_t newToken() {return ++token_;};
};

#endif // DATASERVER_H
